﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DexComanda.Models;

namespace DexComanda
{
    public partial class frmClientesDelivery : Form
    {
        Conexao con;
        public frmClientesDelivery()
        {
            con = new Conexao();
            InitializeComponent();         
        }

        private void btSalvar_Click(object sender, EventArgs e)
        {
            
        }

        private void frmCadastroCliente_Load(object sender, EventArgs e)
        {
            
        }

        private void ConsultarEnderecoPorCep(object sender, KeyPressEventArgs e)
        {
            ConsultaClientesPorCep(e);
      
        }

        private void ConsultaClientesPorCep(KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                //List<String> list = new List<String>();
                if (this.txtCEP.Text.Equals(""))
                {
                    MessageBox.Show("Informe o Cep.");
                }
                else
                {
                    int cep = int.Parse(this.txtCEP.Text);
                    DataSet endereco = con.SelectEnderecoPorCep("base_cep", "spObterEnderecoPorCep", cep);

                    if (endereco.Tables["base_cep"].Rows.Count > 0)
                    {

                        DataRow dRow = endereco.Tables["base_cep"].Rows[0];

                        this.txtEndereco.Text = dRow.ItemArray.GetValue(0).ToString();
                        this.txtBairro.Text = dRow.ItemArray.GetValue(1).ToString();
                        this.txtCidade.Text = dRow.ItemArray.GetValue(2).ToString();
                        this.txtEstado.Text = dRow.ItemArray.GetValue(3).ToString();
                    }
                    else
                    {
                        MessageBox.Show("Cep não cadastrado");

                        this.txtEndereco.Focus();
                    }
                }
            }
        }

        private void AdicionarCliente(object sender, EventArgs e)
        {
            AdicionarCliente();
        }

        private void AdicionarCliente()
        {
            int _cep = int.Parse(this.txtCEP.Text);

            ClienteDelivery Cliente = new ClienteDelivery()
            {

                Nome = this.txtNomeCliente.Text,
                Endereco = this.txtEndereco.Text,
                Numero = int.Parse(this.txtNumero.Text),
                Bairro = this.txtBairro.Text,
                Cidade = this.txtCidade.Text,
                Cep = _cep,
                PontoReferencia = this.txtPontoReferencia.Text,
                Observacao = this.txtObservacaoCliente.Text,
                Telefone = this.txtTelefone.Text,
                UF = this.txtEstado.Text,
            };

            con.Insert("spAdicionarClienteDelivery", Cliente);
            MessageBox.Show("Cliente cadastrado com sucesso.");
            ClearForm(this);
        }

        private static void ClearForm(System.Windows.Forms.Control parent)
        {
            foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
            {
                //Loop through all controls 
                if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
                {
                    //Check to see if it's a textbox 
                    ((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
                    //If it is then set the text to String.Empty (empty textbox) 
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
                {
                    //If its a RichTextBox clear the text
                    ((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
                {
                    //Next check if it's a dropdown list 
                    ((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
                    //If it is then set its SelectedIndex to 0 
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
                {
                    //Next uncheck all checkboxes
                    ((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
                {
                    //Unselect all RadioButtons
                    ((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
                }
                if (ctrControl.Controls.Count > 0)
                {
                    //Call itself to get all other controls in other containers 
                    ClearForm(ctrControl);
                }
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPontoReferencia_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtCEP_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar ==13)
            {
                ConsultaClientesPorCep(e);
            }
         
        }

        private void btnAdicionarCliente_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void btnAdicionarCliente_Click(object sender, EventArgs e)
        {
            AdicionarCliente();
        }

    }
}
